package com.whsxt.sys.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whsxt.common.CodeMsg;
import com.whsxt.common.PageInfo;
import com.whsxt.common.Result;
import com.whsxt.common.util.ConstantUtil;
import com.whsxt.sys.data.MenuData;
import com.whsxt.sys.data.RoleData;
import com.whsxt.sys.domain.Permission;
import com.whsxt.sys.domain.Role;
import com.whsxt.sys.service.IPermissionService;
import com.whsxt.sys.service.IRoleService;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 贺炯
 * @since 2019-09-24
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController {
	
	@Autowired
	private IRoleService iRoleService;
	
	@Autowired
	private IPermissionService iPermissionService;
	
	@Autowired
	private IRoleService IRoleService;
	
	/**
	 * 列表查询
	 * @param data
	 * @return
	 */
	@RequestMapping("/list")
	public Object list(RoleData data) {
		IPage<Role> page = new Page<Role>(data.getPage(),data.getLimit());
		QueryWrapper<Role> queryWrapper = new QueryWrapper<Role>();
		queryWrapper.like(StrUtil.isNotBlank(data.getName()), "name", data.getName());
		queryWrapper.like(StrUtil.isNotBlank(data.getRemark()), "remark",data.getRemark());
		iRoleService.page(page, queryWrapper);
		PageInfo pageInfo = new PageInfo(page.getTotal(), page.getRecords());
		return new Result(pageInfo);
	}
	/**
	 * 	新增角色
	 * @return
	 */
	@RequestMapping("/add")
	public Object add(RoleData data) {
		try {
			data.setCreatetime(new Date());
			iRoleService.save(data);
			return new Result(CodeMsg.ROLE_ADD_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.ROLE_ADD_ERROR);
		}
	}
	/**
	 * 	删除单个数据
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteOne")
	public Object deleteOne(Integer id) {
		try {
		//	iRoleService.removeById(id);
			Role role = new Role();
			role.setId(id);
			role.setAvailable(ConstantUtil.AVAILABLE_FALSE);
			IRoleService.updateById(role);
			return new Result(CodeMsg.ROLE_DELETE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.ROLE_DELETE_ERROE);
		}	
	}
	/**
	 * 	更新单个数据
	 * @param data
	 * @return
	 */
	@RequestMapping("/update")
	public Object update(RoleData data) {
		try {
			iRoleService.updateById(data);
			return new Result(CodeMsg.ROLE_UPDATE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.ROLE_UPDATE_ERROE);
		}
	}
	/**
	 * 开启复选框      并选中当前角色拥有的权限  返回树的json数据
	 * @param rid
	 * @return
	 */
	@RequestMapping("/dtree")
	public Object dtree(Integer rid) {
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
		queryWrapper.eq("available", ConstantUtil.AVAILABLE_TRUE);
		//查询所以的菜单及权限
		List<Permission> list = iPermissionService.list(queryWrapper);
		//用于装载 页面菜单所需要的 json 的容器
		List<MenuData> datas = new ArrayList<MenuData>();
		//查询当前角色已经拥有的权限
		List<Permission> currPermissionList = iRoleService.selectPermissionByRid(rid);
		//遍历 如果 list 中的对象 跟 currPermissionList 中的对象的 id值相等 说明该角色有该权限 给 checkArr 属性 赋值为"0"
		for (Permission p : list) {
			String checkArr = "0";
			for (Permission p2 : currPermissionList) {
				if(p2.getId() == p.getId()) {
					checkArr = "1";
					break;
				}
			}
			Boolean spread = (p.getOpen()==null||p.getOpen()==1)?true:false;
			datas.add(new MenuData(p.getId(), p.getPid(), p.getTitle(), spread, checkArr));
		}
		return new Result(datas);
	}
	/**
	 * 保存前端设置的权限   先删除该角色拥有的权限 然后在批量插入 这在在service中进行的操作
	 * @param rid
	 * @param pid
	 * @return
	 */
	@RequestMapping("/aod")
	public Object aod(Integer rid,Integer [] pid) {
		
		try {
			iRoleService.savePidByRid(rid,pid);
			return new Result(CodeMsg.SAVE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.SAVE_ERROR);
			}
	}
}

